﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace OnlineBusTicket.User
{
    public partial class ChangeRoleName : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                ddlRole.DataSource = Roles.GetAllRoles();
                ddlRole.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var users = Roles.GetUsersInRole(ddlRole.SelectedValue);


            if (Roles.RoleExists(txtNewRoleName.Text) == false)
            {
                Roles.CreateRole(txtNewRoleName.Text);//tao rolename moi

                // chi dinh user de gan quyen, thay doi tat ca user co role cu thanh role moi
                if (users.Length > 0)
                {
                    Roles.AddUsersToRole(users, txtNewRoleName.Text);
                    Roles.RemoveUsersFromRole(users, ddlRole.SelectedValue);
                }

                Roles.DeleteRole(ddlRole.SelectedValue);//xoa rolename cu
            }
            else
            {
                lblStatus.Text=("New role exist");
                txtNewRoleName.Focus();
            }
            Response.Redirect("ListAllUserRole.aspx");
        }
    }
}